// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: LerpStack.cpp
// Desrciption	: CLerpStack implementation file.
// Author(s)	: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "SolitaireStd.h"

// Library Includes

// This Include
#include "LerpStack.h"

// Local Includes

CLerpStack::CLerpStack(const CCardStack& _krStack,
					   CCardStack* _pDestinationStack,
					   float _fWaitTime,
					   bool _bFlipOnceDone,
					   float _fMovementSpeed)
	:m_fElapsedTime(0.0f),
	m_fWaitTime(_fWaitTime),
	m_fMovementSpeed(_fMovementSpeed),
	m_pDestinationStack(_pDestinationStack),
	m_bFlipOnceDone(_bFlipOnceDone)
{
	// Transfer CardStack to LerpStack.
	m_position = _krStack.GetPosition();
	m_iOffset = _krStack.GetOffset();
	m_eDirection = _krStack.GetDirection();
	m_cards = _krStack.GetStack();

	m_startPosition = m_position;

	if(m_pDestinationStack) {
		// Retrieve the destination position.
		m_endPosition = m_pDestinationStack->VGetNextPosition();

		m_pDestinationStack->PushLerping(m_cards);
	}
}

CLerpStack::CLerpStack(const CPlayingCard& _krCard,
					   CCardStack* _pDestinationStack,
					   float _fWaitTime,
					   bool _bFlipOnceDone,
					   float _fMovementSpeed)
	:m_fElapsedTime(0.0f),
	m_fWaitTime(_fWaitTime),
	m_fMovementSpeed(_fMovementSpeed),
	m_pDestinationStack(_pDestinationStack),
	m_bFlipOnceDone(_bFlipOnceDone)
{
	m_position = _krCard.GetPosition();
	m_iOffset = 0;
	m_eDirection = keDown;

	m_cards.push_back(_krCard);

	m_startPosition = m_position;

	if(m_pDestinationStack) {
		// Retrieve the destination position.
		m_endPosition = m_pDestinationStack->VGetNextPosition();

		m_pDestinationStack->PushLerping(_krCard);
	}
}

CLerpStack::~CLerpStack()
{

}

void CLerpStack::Update(float _fDeltaTime) 
{
	m_fElapsedTime += _fDeltaTime;

	if(m_fElapsedTime <= m_fWaitTime) {
	// We haven't surpassed the wait time yest.
		return;
	}

	if(!m_pDestinationStack) {
		// Without a destination we have no where to go.
		return;
	}

	float fT = m_fElapsedTime - m_fWaitTime;	// The time we've surpassed the wait time.

	if(fT > m_fMovementSpeed) {
	// We are done moving.
//		return;
	}

	fT *= m_fMovementSpeed;

	if(fT <= 1 && fT > 0) {
		if(fT > 0.75f) {	// Are we at 75%?
		// This makes it so that flipping isn't jarring.
			if(m_bFlipOnceDone) {
				for(unsigned int i = 0; i < m_cards.size(); ++i) {
					m_cards[i].Flip();
				}
				m_bFlipOnceDone = false;
			}
		}
		// Move position with linear interpolation.
		m_position = Lerp(m_startPosition, m_endPosition, fT);
	
		VUpdatePositions();
	}
}

void CLerpStack::PushOntoDest()
{
	if(m_pDestinationStack) {
		if(m_bFlipOnceDone) {
				for(unsigned int i = 0; i < m_cards.size(); ++i) {
					m_cards[i].Flip();
				}
				m_bFlipOnceDone = false;
		}
		// Push this stack onto it's destination stack.
		m_pDestinationStack->Push(m_cards);

		// Pop all of these lerping cards of the lerping stack.
		for(unsigned int i = 0; i < m_cards.size(); ++i) {
			m_pDestinationStack->PopLerping();
		}
	}
}

bool CLerpStack::IsInPlace() const
{
	if((m_fElapsedTime - m_fWaitTime) * m_fMovementSpeed >= 1) {
		return true;
	}
	else {
		return false;
	}
}